package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Glry;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GlryForm extends BasePage implements Serializable {
    private GenericManager<Glry, Long> glryManager;
    private Glry glry = new Glry();
    private Long id;
    private Jbqk jbqk;
    public void setJbqk(Jbqk jbqk)
    {
    	this.jbqk=jbqk;
    }

    public void setGlryManager(GenericManager<Glry, Long> glryManager) {
        this.glryManager = glryManager;
    }

    public Glry getGlry() {
        return glry;
    }

    public void setGlry(Glry glry) {
        this.glry = glry;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        glryManager.remove(glry.getId());
        addMessage("glry.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            glry = glryManager.get(id);
        } else {
            glry = new Glry();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (glry.getId() == null || glry.getId() == 0);
        
        glry.setJbqk(jbqk);
        glry.setUsername(getRequest().getRemoteUser());
        glry.setOptdate(new Date());
        glry.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        
        glryManager.save(glry);

        String key = (isNew) ? "glry.added" : "glry.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	  this.id=this.glry.getId();
              return edit();

        }
    }
} 